package org.agile2soft.nexoma.tapestry.pages;

import java.util.List;

import org.agile2soft.nexoma.core.controllers.ServiceController;
import org.agile2soft.nexoma.core.controllers.ServiceOptionController;
import org.agile2soft.nexoma.core.controllers.ServicesGroupController;
import org.agile2soft.nexoma.core.entities.Service;
import org.agile2soft.nexoma.core.entities.ServiceOption;
import org.agile2soft.nexoma.core.entities.ServicesGroup;
import org.agile2soft.nexoma.tapestry.model.ApplicationContainer;
import org.agile2soft.nexoma.tapestry.model.SessionContainer;
import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;

@IncludeJavaScriptLibrary("context:resources/js/Portal.js")
public class Portal {

    @Persist(PersistenceConstants.FLASH)
    private String messageCode;

    public String getMessageCode ( String message ) {
        return messageCode;
    }

    public void setMessageCode ( String messageCode ) {
        this.messageCode = messageCode;
    }

    @Inject
    private Messages _messages;

    public String getMessage() {
        return ( messageCode!=null ) ? _messages.get(messageCode) : null;
    }

    @Inject
    private RenderSupport _renderSupport;

    @AfterRender
    void afterRender() {
        _renderSupport.addInit("portalmanager","portalmanager");
    }

    @Inject
    @Property
    private ApplicationContainer appContainer;

    @Property
    private ServicesGroup group;

    @Property
    private Service service;

    @Property
    private ServiceOption option;

    @Inject
    private ServiceController serviceController;

    @Inject
    private ServicesGroupController serviceGroupController;

    @Inject
    private ServiceOptionController optionController;

    public List<ServicesGroup> getGroups () {
        return serviceGroupController.findAll();
    }

    public List<Service> getServices() {
        return serviceController.findByGroup(group);
    }

    public List<ServiceOption> getOptions() {
        return optionController.findByService(service);
    }
    
    @SessionState
    private SessionContainer session;
    
    @Inject
    private Logger logger;
    
    @OnEvent(value=EventConstants.ACTIVATE)
    boolean doInitPortal(String command) {
    	if ( command.equals("init") ) {
    		session.clearAll();
    	}
    	
    	return true;
    }
//    
//    @OnEvent(value=EventConstants.ACTIVATE)
//    boolean doInitPortal(Integer serviceGroupId) {
//    	logger.info("Portal service init : " + serviceGroupId);
//    	session.setCurrentGroup(serviceGroupController.findById(serviceGroupId));
//    	return true;
//    }
//
//    @OnEvent(value=EventConstants.ACTIVATE)
//    boolean doInitPortal(Integer serviceGroupId , Integer serviceId) {
//    	logger.info("Portal service init : " + serviceGroupId + "," +serviceId);
//    	session.setCurrentGroup(serviceGroupController.findById(serviceGroupId));
//    	session.setCurrentService(serviceController.findById(serviceId));
//    	return true;
//    }
//    
//    @OnEvent(value=EventConstants.PASSIVATE)
//    List<Integer> doPassivate () {
//    	return session.getCurrentSelection();
//    }
    
}
